package com.cnstrong.webview2.view;

import android.graphics.Rect;
import android.view.View;

public class MagicGesture {
    private static final int SPOT_OFFSET = 200;

    private float mUpX;
    private float mUpY;
    private float mDownX;
    private float mDownY;

    private final Rect mLeftTopRect;
    private final Rect mRightTopRect;
    private final Rect mLeftBottomRect;
    private final Rect mRightBottomRect;

    private final Rect mClickRefreshRect;

    private final Rect mLeftLineRect;
    private final Rect mRightLineRect;

    public static final int TYPE_NONE = -1;
    public static final int TYPE_SPOT = 0;
    public static final int TYPE_LINE = 1;

    private int mGestureType = TYPE_NONE;

    public int getGestureTyp() {
        return mGestureType;
    }

    public MagicGesture(View view, float downSpotX, float downSpotY, float upSpotX, float upSpotY) {
        mDownX = downSpotX;
        mDownY = downSpotY;
        mUpX = upSpotX;
        mUpY = upSpotY;

        // 左上角有效矩形区域
        mLeftTopRect = new Rect(
                0,
                0,
                SPOT_OFFSET,
                SPOT_OFFSET / 2); // 高度

        // 左下角有效矩形区域
        mLeftBottomRect = new Rect(
                0,
                view.getHeight() - SPOT_OFFSET / 2,  // 高度
                SPOT_OFFSET,
                view.getHeight());

        // 右上角有效矩形区域
        mRightTopRect = new Rect(
                view.getWidth() - SPOT_OFFSET * 2 - (int) (SPOT_OFFSET * 0.8),
                0,
                view.getWidth() - SPOT_OFFSET - (int) (SPOT_OFFSET * 0.8), // 高度
                SPOT_OFFSET / 2);

        // 右下角有效矩形区域
        mRightBottomRect = new Rect(
                view.getWidth() - SPOT_OFFSET / 2,
                view.getHeight() - SPOT_OFFSET / 2, // 高度
                view.getWidth(),
                view.getHeight());

        // 左线点有效矩形区域
        mLeftLineRect = new Rect(
                0,
                0,
                SPOT_OFFSET,
                view.getHeight());

        // 右线点有效矩形区域
        mRightLineRect = new Rect(
                view.getWidth() - SPOT_OFFSET,
                0,
                view.getWidth(),
                view.getHeight());

        // 判断类型
        if (isLeftTop() || isLeftBottom() || isRightTop() || isRightBottom()) {
            mGestureType = TYPE_SPOT;
        } else if (isLineGesture()) {
            mGestureType = TYPE_LINE;
        }

        // 点击刷新区域
        mClickRefreshRect = new Rect(
                view.getWidth() / 2 - SPOT_OFFSET,
                view.getHeight() / 2 - SPOT_OFFSET,
                view.getWidth() / 2 + SPOT_OFFSET,
                view.getHeight() / 2 + SPOT_OFFSET);
    }

    /**
     * 判断是不是左上角点区域
     *
     * @return
     */
    public boolean isLeftTop() {
        return mLeftTopRect.contains((int) mDownX, (int) mDownY) && mLeftTopRect.contains((int) mUpX, (int) mUpY);
    }

    /**
     * 判断是不是左下角区域
     *
     * @return
     */
    public boolean isLeftBottom() {
        return mLeftBottomRect.contains((int) mDownX, (int) mDownY) && mLeftBottomRect.contains((int) mUpX, (int) mUpY);
    }

    /**
     * 判断是不是右上角区域
     *
     * @return
     */
    public boolean isRightTop() {
        return mRightTopRect.contains((int) mDownX, (int) mDownY) && mRightTopRect.contains((int) mUpX, (int) mUpY);
    }

    /**
     * 判断是不是右下角区域
     *
     * @return
     */
    public boolean isRightBottom() {
        return mRightBottomRect.contains((int) mDownX, (int) mDownY) && mRightBottomRect.contains((int) mUpX, (int) mUpY);
    }

    /**
     * 判断直线手势
     *
     * @return
     */
    public boolean isLineGesture() {
        return mLeftLineRect.contains((int) mDownX, (int) mDownY) && mRightLineRect.contains((int) mUpX, (int) mUpY);
    }

    /**
     * 判断点击刷新
     *
     * @return
     */
    public boolean isRefreshGesture() {
        return mClickRefreshRect.contains((int) mDownX, (int) mDownY) && mClickRefreshRect.contains((int) mUpX, (int) mUpY);
    }

    @Override
    public String toString() {
        return "MagicGesture{" +
                "mUpX=" + mUpX + "\n" +
                ", mUpY=" + mUpY + "\n" +
                ", mDownX=" + mDownX + "\n" +
                ", mDownY=" + mDownY + "\n" +
                ", mLeftTopRect=" + mLeftTopRect + "\n" +
                ", mRightTopRect=" + mRightTopRect + "\n" +
                ", mLeftBottomRect=" + mLeftBottomRect + "\n" +
                ", mRightBottomRect=" + mRightBottomRect + "\n" +
                ", mClickRefreshRect=" + mClickRefreshRect + "\n" +
                ", mLeftLineRect=" + mLeftLineRect + "\n" +
                ", mRightLineRect=" + mRightLineRect + "\n" +
                ", mGestureType=" + mGestureType + "\n" +
                '}';
    }

    public Rect getLeftTopRect() {
        return mLeftTopRect;
    }

    public Rect getRightTopRect() {
        return mRightTopRect;
    }

    public Rect getLeftBottomRect() {
        return mLeftBottomRect;
    }

    public Rect getRightBottomRect() {
        return mRightBottomRect;
    }

    public Rect getClickRefreshRect() {
        return mClickRefreshRect;
    }
}
